﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Collections;
using BPERPHelpTool.DBHelp;
using System.Configuration;
using System.Threading;


namespace BPERPHelpTool
{

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.backgroundWorker1.RunWorkerAsync();
        }

        //实例化公共类的对象
        DataBaseHelp db = new DataBaseHelp();

        private void btn_Copy_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(cBType.Text.Trim() + " | " + cBApp.Text.Trim() + " | "
                + cBDept.Text.Trim() + " | " + cBProbType.Text.Trim() + " | " + txtDec.Text.Trim());
            MessageBox.Show("复制成功，你现在可以直接粘贴到任何文本编辑器中了");
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            cBType.Text = null;
            cBApp.Text = null;
            cBDept.Text = null;
            cBProbType.Text = null;
            txtDec.Text = null;
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            //string strText = cBType.Text.Trim() + " | " + cBApp.Text.Trim() + " | "
            //    + cBDept.Text.Trim() + " | " + cBProbType.Text.Trim() + " | " + txtDec.Text.Trim();
            //System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            //Stream stream = new FileStream("MyFile.txt", FileMode.Create, FileAccess.Write, FileShare.None);
            //formatter.Serialize(stream, strText);
            //stream.Close(); 
            //SaveFileDialog();
            OpenFileDialog();
        }

        #region 保存对话框
        private void SaveFileDialog()
        {
            //string localFilePath, fileNameExt, newFileName, FilePath;  
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            //设置文件类型  
            saveFileDialog1.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*";

            //设置默认文件类型显示顺序  
            saveFileDialog1.FilterIndex = 1;

            //保存对话框是否记忆上次打开的目录  
            saveFileDialog1.RestoreDirectory = true;

            //点了保存按钮进入  
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //获得文件路径  
                //localFilePath = saveFileDialog1.FileName.ToString();  

                //获取文件名，不带路径  
                //fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);  

                //获取文件路径，不带文件名  
                //FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));  

                //给文件名前加上时间  
                //newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt;  

                //在文件名里加字符  
                //saveFileDialog1.FileName.Insert(1, "Zhaoyi");  

                System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();//输出文件  

                //fs输出带文字或图片的文件，就看需求了  
            }

        }
        #endregion


        public void OpenFileDialog()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "文本文件(*.txt)|*.txt";
            DialogResult dr = ofd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                //richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);
                string strText = cBType.Text.Trim() + " | " + cBApp.Text.Trim() + " | "
                             + cBDept.Text.Trim() + " | " + cBProbType.Text.Trim() + " | " + txtDec.Text.Trim();

            }
            ofd.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            //创建链接
            //string strConn = "Data Source=192.168.48.2;Initial Catalog=CNLY_App;User ID=sa;Password=12345;MultipleActiveResultSets=True";

            //创建sql 语句
            //string strsql = "select top 100  GroupName,GroupDesc from GroupNames";
            //SqlConnection conn = new SqlConnection(strConn);
            //conn.Open();
            //SqlDataAdapter da = new SqlDataAdapter(strsql, conn);
            //DataSet ds = new DataSet();
            //da.Fill(ds);
            //DataTable dt = ds.Tables[0];

            string struser = textBox1.Text;
            string strdepart = comboBox1.Text;
            DataTable dt = null;
            dt = DataBaseHelp.GetUserDetailTable(struser, strdepart);
            dataGridView1.DataSource = dt;
            
        }


        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //循环执行
            while (true)
            {
                int repCNLYCount = DataBaseHelp.GetCNLYRepRow();
                int repCNBTCount = DataBaseHelp.GetCNBTRepRow();
                int repCNP1Count = DataBaseHelp.GetCNP1RepRow();
                int repCNP2Count = DataBaseHelp.GetCNP2RepRow();
                int repUSMACount = DataBaseHelp.GetUSMARepRow();
                int repTWHUCount = DataBaseHelp.GetTWHURepRow();


                RepModels models = new RepModels();
                models.CNLYRepModels = repCNLYCount;
                models.CNBTRepModels = repCNBTCount;
                models.CNP1RepModels = repCNP1Count;
                models.CNP2RepModels = repCNP2Count;
                models.USMARepModels = repUSMACount;
                models.TWHURepModels = repTWHUCount;

                //执行后台任务
                this.backgroundWorker1.ReportProgress(0, models);
                Thread.Sleep(BPSettings.Default.RepeatWaitMin * 60 * 1000);
            }
        }


        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            RepModels models = (RepModels)e.UserState;
            this.label12.Text = models.CNLYRepModels.ToString();
            this.label13.Text = models.CNBTRepModels.ToString();
            this.label14.Text = models.CNP1RepModels.ToString();
            this.label15.Text = models.CNP2RepModels.ToString();
            this.label17.Text = models.USMARepModels.ToString();
            this.label19.Text = models.TWHURepModels.ToString();

            if (BPSettings.Default.ShownWhenBlock)
            {
                if (models.CNLYRepModels == 0 && models.CNBTRepModels ==0 && models.CNP1RepModels ==0 && models.CNP2RepModels ==0)
                {
                    this.Visible = false;
                }
                else
                {
                    this.Visible = true;
                }
            }
            else
            {
                this.Visible = true;
            }
        }

    }
}
